﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockBusiness;
using MockCommon;
using MockDataTransferObject;
using System.Globalization;

namespace Presentation
{
    public partial class ViewContracts : System.Web.UI.Page
    {
        private ContractBusiness _contractBusiness;
        private ContractTypeBusiness _contractTypeBusiness;
        private ConsultantBusiness _consultantBusiness;
        private Label _warningLabel;
        private Label _errorLabel;
        private Label _successLabel;
        private CheckBox _allSearchCheckBox;
        private DropDownList _statusDropDownList;
        private DropDownList _contractTypeDropDownList;
        private Button _filterButton;
        private ListView _contractListView;
        private Button _showButton;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                _contractBusiness = ContractBusiness.RetrieveInstance();
                _contractTypeBusiness = ContractTypeBusiness.RetrieveInstance();
                _consultantBusiness = ConsultantBusiness.RetrieveInstance();
                _warningLabel = (Label)Master.FindControl("WarningLabel");
                _errorLabel = (Label)Master.FindControl("ErrorLabel");
                _successLabel = (Label)Master.FindControl("SuccessLabel");
                _allSearchCheckBox = (CheckBox)ListContractUserControl.FindControl("AllSearchCheckBox"); 
                _statusDropDownList = (DropDownList)ListContractUserControl.FindControl("StatusDropDownList");
                _contractTypeDropDownList = (DropDownList)ListContractUserControl.FindControl("ContractTypeDropDownList");
                _filterButton = (Button)ListContractUserControl.FindControl("FilterButton");
                _contractListView = (ListView)ListContractUserControl.FindControl("ContractListView");
                _showButton = (Button)ListContractUserControl.FindControl("ShowButton");
                _allSearchCheckBox.CheckedChanged += new EventHandler(AllSearchCheckBox_CheckedChanged);
                _statusDropDownList.SelectedIndexChanged += new EventHandler(StatusDropDownList_SelectedIndexChanged);
                _contractTypeDropDownList.SelectedIndexChanged += new EventHandler(ContractTypeDropDownList_SelectedIndexChanged);
                _filterButton.Click += new EventHandler(FilterButton_Click);
                _showButton.Click += new EventHandler(ShowButton_Click);
            }
            catch (MockException exception)
            {
                Application["error"] = exception;
                Response.Redirect("Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["user"] != null)
            {
                User user = (User)Session["user"];
                if (user.Role.RoleName == "Consultant")
                {
                    if (!IsPostBack)
                    {
                        Loading();
                    }
                }
                else
                {
                    Response.Redirect("NotAuthorized.aspx");
                }
            }
            else
            {
                Response.Redirect("LogOn.aspx");
            }
            if (IsPostBack)
            {
                _warningLabel.Text = "";
                _errorLabel.Text = "";
                _successLabel.Text = "";
            }
        }

        public void Loading()
        {
            try
            {
                User user = (User)Session["user"];
                object consultantId = _consultantBusiness.GetConsultantIdByUserName(user.UserName);
                _contractListView.DataSource = _contractBusiness.GetByConsultantId(consultantId);
                _contractListView.DataBind();
                _contractTypeDropDownList.DataSource = _contractTypeBusiness.RetrieveAllContractType();
                _contractTypeDropDownList.DataValueField = "ContractTypeId";
                _contractTypeDropDownList.DataTextField = "ContractTypeName";
                _contractTypeDropDownList.DataBind();
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void AllSearchCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (_allSearchCheckBox.Checked)
            {
                _statusDropDownList.AutoPostBack = false;
                _contractTypeDropDownList.AutoPostBack = false;
            }
            else
            {
                _statusDropDownList.AutoPostBack = true;
                _contractTypeDropDownList.AutoPostBack = true;
            }
        }

        protected void ShowButton_Click(object sender, EventArgs e)
        {
            try
            {
                User user = (User)Session["user"];
                object consultantId = _consultantBusiness.GetConsultantIdByUserName(user.UserName);
                _contractListView.DataSource = _contractBusiness.GetByConsultantId(consultantId);
                _contractListView.DataBind();
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void FilterButton_Click(object sender, EventArgs e)
        {
            try
            {
                User user = (User)Session["user"];
                object consultantId = _consultantBusiness.GetConsultantIdByUserName(user.UserName);
                object contractType = _contractTypeDropDownList.SelectedValue;
                int contractStatus = Int32.Parse(_statusDropDownList.SelectedValue, CultureInfo.CurrentCulture);
                bool isActive = contractStatus == 1 ? true : false;
                object source = null;
                source = _contractBusiness.GetByFilter(contractType, isActive, consultantId);
                _contractListView.DataSource = source;
                _contractListView.DataBind();
            }
            catch (ArgumentException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void ContractTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                User user = (User)Session["user"];
                object consultantId = _consultantBusiness.GetConsultantIdByUserName(user.UserName);
                object contractType = _contractTypeDropDownList.SelectedValue;
                object source = null;
                source = _contractBusiness.GetByFilter(contractType, "", consultantId);
                _contractListView.DataSource = source;
                _contractListView.DataBind();
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void StatusDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                User user = (User)Session["user"];
                object consultantId = _consultantBusiness.GetConsultantIdByUserName(user.UserName);
                int contractStatus = Int32.Parse(_statusDropDownList.SelectedValue, CultureInfo.CurrentCulture);
                bool isActive = contractStatus == 1 ? true : false;
                object source = null;
                source = _contractBusiness.GetByFilter("", isActive, consultantId);
                _contractListView.DataSource = source;
                _contractListView.DataBind();
            }
            catch (ArgumentException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }
    }
}